package com.xjj.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.common.R;
import com.xjj.entity.Category;
import com.xjj.entity.Dish;
import com.xjj.entity.Employee;
import com.xjj.service.CategoryService;
import com.xjj.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);

        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();

        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Category::getName,name);
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);

        //执行查询
        categoryService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
/**
 * 获取菜品分类列表
 */
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        categoryLambdaQueryWrapper.select(Category::getName).eq(Category::getType,type);
        categoryLambdaQueryWrapper.eq(type!=null,Category::getType,type);
        //添加排序条件
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);

        return R.success(list);
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Category category){

//        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        categoryLambdaQueryWrapper.eq(Category::getName,category.getName());
//        Category one = categoryService.getOne(categoryLambdaQueryWrapper);
//        if (one!=null){
//            return R.error("新增分类失败：该分类已存在！");
//        }
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
        log.info("新增分类，分类信息：{}",category.toString());
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除分类id"+id);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        Dish one = dishService.getOne(dishLambdaQueryWrapper);
        if (one!=null){
            return R.error("删除分类失败，请先删除该分类下的菜品或套餐！");
        }
        categoryService.removeById(id);
        return R.success("删除分类成功！");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);

//        category.setUpdateTime(LocalDateTime.now());
        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }
}

